package org.me.marble;

import java.nio.*;

import java.util.ArrayList;
import javax.microedition.khronos.opengles.GL10;

/**
 *
 * @author tungi
 */

public class FastMarkObject extends AvailableObject{
    private FloatBuffer frontBuffer,backBuffer,blanketBuffer;
    private FloatBuffer frontTextureBuffer,backTextureBuffer;
    private MyColor color_B;
    private int res;

    public FastMarkObject(int id){
        super(id);
    }

    @Override
    public void effect(GameRenderer _renderer,MoveMarble _mm){
        _mm.modifySpeed(2);
        _renderer.addRunnable(timerTypes.fast.ordinal(),delay);
    }

    @Override
    public void init(){        
        float rad=0.25f,a,b;
        if(OptionsLibrary.objects_quality.equals("low"))
            res=10;
        else if(OptionsLibrary.objects_quality.equals("medium"))
            res=20;
        else if(OptionsLibrary.objects_quality.equals("high"))
            res=30;

        ArrayList<Float> front_circle=new ArrayList<Float>();
        ArrayList<Float> back_circle=new ArrayList<Float>();
        ArrayList<Float> blanket=new ArrayList<Float>();
        ArrayList<Float> fronttexture=new ArrayList<Float>();
        ArrayList<Float> backtexture=new ArrayList<Float>();

        for(int i=0;i<=res;++i){
            a=rad*(float)Math.cos(2*Math.PI*i/res);
            b=rad*(float)Math.sin(2*Math.PI*i/res);

            front_circle.add(a);
            front_circle.add(b);
            front_circle.add(-0.02f);

            back_circle.add(0,a);
            back_circle.add(1,b);
            back_circle.add(2,0.02f);
            
            blanket.add(a);
            blanket.add(b);
            blanket.add(-0.02f);
            blanket.add(a);
            blanket.add(b);
            blanket.add(0.02f);

            a=(float)Math.cos(2*Math.PI*i/res);
            b=(float)Math.sin(2*Math.PI*i/res);
            fronttexture.add(Math.abs(a/2-0.5f));
            fronttexture.add(b/2+0.5f);
            backtexture.add(0,a/2+0.5f);
            backtexture.add(1,b/2+0.5f);
        }

        front_circle.add(0,0.0f);
        front_circle.add(1,0.0f);
        front_circle.add(2,-0.02f);
        fronttexture.add(0,0.5f);
        fronttexture.add(1,0.5f);

        back_circle.add(0,0.0f);
        back_circle.add(1,0.0f);
        back_circle.add(2,0.02f);
        backtexture.add(0,0.5f);
        backtexture.add(1,0.5f);

        frontBuffer=BufferFactory.MakeFloatBuffer(front_circle);
        backBuffer=BufferFactory.MakeFloatBuffer(back_circle);
        blanketBuffer=BufferFactory.MakeFloatBuffer(blanket);
        frontTextureBuffer=BufferFactory.MakeFloatBuffer(fronttexture);
        backTextureBuffer=BufferFactory.MakeFloatBuffer(backtexture);

        color=new MyColor();
        color.setAmbient(new float[]{1.0f,1.0f,1.0f,0.8f});
        color.setDiffuse(new float[]{1.0f,1.0f,1.0f,0.8f});
        color.setSpecular(new float[]{0.6f,0.6f,0.6f,0.7f});
        color.setEmission(new float[]{0.1f,0.1f,0.1f,0.6f});
        color.setShininess(1.0f);

        color_B=new MyColor();
        color_B.setAmbient(new float[]{1.0f,0.0f,0.0f,0.8f});
        color_B.setDiffuse(new float[]{1.0f,0.0f,0.0f,0.8f});
        color_B.setSpecular(new float[]{0.6f,0.6f,0.6f,0.7f});
        color_B.setEmission(new float[]{0.1f,0.1f,0.1f,0.6f});
        color_B.setShininess(1.0f);
    }
    
    @Override
    public void draw(GL10 gl){
        transform(gl);

        OpenGLSystem.enable(gl,OpenGLSystem.CULL_FACE | OpenGLSystem.TEXTURE);
        OpenGLSystem.enableClientStates(gl,OpenGLSystem.NORMAL_ARRAY |
                OpenGLSystem.VERTEX_ARRAY | OpenGLSystem.TEXTURE_ARRAY);

        OpenGLSystem.setMaterial(gl,color);

        OpenGLSystem.drawTriangles(gl,texture,frontBuffer,frontBuffer,
                frontTextureBuffer,res+2,GL10.GL_TRIANGLE_FAN);
        OpenGLSystem.drawTriangles(gl,texture,backBuffer,backBuffer,
                backTextureBuffer,res+2,GL10.GL_TRIANGLE_FAN);

        OpenGLSystem.disableClientStates(gl,OpenGLSystem.TEXTURE_ARRAY);
        OpenGLSystem.disable(gl,OpenGLSystem.TEXTURE);

        OpenGLSystem.setMaterial(gl,color_B);
        OpenGLSystem.drawTriangles(gl,null,blanketBuffer,blanketBuffer,
                null,2*(res+1),GL10.GL_TRIANGLE_STRIP);

        OpenGLSystem.disableClientStates(gl,OpenGLSystem.NORMAL_ARRAY | 
                OpenGLSystem.VERTEX_ARRAY);
        OpenGLSystem.disable(gl,OpenGLSystem.CULL_FACE);
    }
}
